package com.doraro.platform.module.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doraro.platform.common.beans.params.PageParam;
import com.doraro.platform.common.beans.response.PageView;
import com.doraro.platform.common.utils.Result;
import com.doraro.platform.common.utils.ShiroUserUtil;
import com.doraro.platform.module.device.model.entity.RepairOrder;
import com.doraro.platform.module.device.service.IRepairOrderService;
import com.doraro.platform.module.user.model.entity.SysJob;
import com.doraro.platform.module.user.model.vo.CompleteParam;
import com.doraro.platform.module.user.model.vo.JobCond;
import com.doraro.platform.module.user.service.ISysJobService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-06-18
 */
@RestController
@RequestMapping("/api/job")
public class SysJobRestController {
    @Autowired
    private ISysJobService jobService;
    @Autowired
    private IRepairOrderService orderService;

    @GetMapping("/current")
    @ApiOperation("用户是否工作")
    @RequiresPermissions("engineer:job")
    public Result job(String userId) {
        if (userId == null) {
            userId = ShiroUserUtil.getCurrentId();
        }
        final SysJob job = jobService.unCompleteJob(userId);
        if (job == null) {
            return new Result().ok();
        }
        final RepairOrder order = orderService.getById(job.getOrderId());
        return new Result().ok()
                .addData("job", job)
                .addData("order", order);
    }


    @GetMapping("/historyJob")
    @ApiOperation("工程师历史工作")
    @RequiresPermissions("engineer:historyJob")
    public Result historyJob(String userId, JobCond job, PageParam<SysJob> pageParam) {
        if (userId == null) {
            userId = ShiroUserUtil.getCurrentId();
        }
        final LambdaQueryWrapper<SysJob> wrapper = job.condition()
                .eq(SysJob::getUid, userId)
                .eq(SysJob::getJobStatus, 2);
        final IPage<SysJob> page = jobService.page(pageParam.toPage(), wrapper);
        return new Result().ok().addData("page", new PageView(page));
    }

    @PostMapping("/complete")
    @ApiOperation("工程师发送完成维修请求")
    @RequiresPermissions("order:complete")
    public Result complete(@Validated @RequestBody CompleteParam param) {
        final String engineerId = ShiroUserUtil.getCurrentId();
        final Integer id = jobService.completeJob(engineerId, param);
        return new Result().ok().addData("solutionId", id);
    }

    /**
     * 仅在用户未确认的时候可以修改
     *
     * @param param
     * @return
     */
    @PutMapping("/complete")
    @ApiOperation("工程师更新维修详情")
    @RequiresPermissions("order:updateComplete")
    public Result updateCompleteInfo(@Validated @RequestBody CompleteParam param) {
        final String engineerId = ShiroUserUtil.getCurrentId();
        final Integer solutionId = jobService.updateSolution(engineerId, param);
        return new Result().ok().addData("solutionId", solutionId);
    }
}

